<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Model\Join;
use App\Http\Controllers\Controller;

class JoinController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $joins = Join::all();

        return view('admin.join.list',['joins' => $joins]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.join.form');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
                'title' => 'required',
                'nums' => 'required',
                'img' => 'required',
                'duty' => 'required'
            ]);

        $file = $request->file('img');
        $file_name = $file->getClientOriginalName();

        if (!$file->move('joins/', $file_name)) {

            return view('admin.join.form');
        }
        $join = new Join();

        $join->title = $request->input('title');
        $join->nums = $request->input('nums');
        $join->img = 'joins/' . $file_name;
        $join->duty = json_encode($request->input('duty'));


        if ($join->save()) {

            return redirect('admin/joins');
        }else{

            return view('admin.join.form', $join);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $join = Join::find($id);
        return view('admin.join.form', $join);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $join = Join::find($id);

        $this->validate($request, [
                'title' => 'required',
                'nums' => 'required',
                //'img' => 'required',
                'duty' => 'required'
            ]);

        $file_name = '';

        if ($request->file('img')) {
            
            $file = $request->file('img');
            $file_name = $file->getClientOriginalName();

            if (!$file->move('joins/', $file_name)) {

                return view('admin.join.form');
            }
        }
        

        $join->title = $request->input('title');
        $join->nums = $request->input('nums');
        $join->img = $file_name ? ('joins/' . $file_name) : $join->img;
        $join->duty = json_encode($request->input('duty'));


        if ($join->save()) {

            return redirect('admin/joins');
        }else{

            return view('admin.join.form', $join);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Join::destroy($id);
        return redirect('/admin/joins');
    }
}
